package codeTop;

public class q2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode p = head;
        int plus = 0;
        while (l1 != null || l2 != null) {
            int num1 = 0;
            int num2 = 0;
            if (l1 != null) {
                num1=l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                num2 = l2.val;
                l2 = l2.next;
            }
            p.next  = new ListNode((num1+num2+plus)%10);
            plus = (num1+num2+plus)/10;
            p = p.next;
        }
        if (plus == 1) {
            p.next = new ListNode(1);
        }
        return head.next;
    }
}
